package jangdolbaeng2.seller.dao;

import jangdolbaeng2.db.MybatisConfig;
import jangdolbaeng2.vo.Qa_commentVO;
import jangdolbaeng2.vo.Shop_noticeVO;
import jangdolbaeng2.vo.Shop_qaVO;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


//게시판 관련
public class BoardDAO {
	private SqlSessionFactory factory = MybatisConfig.getSqlSessionFactory(); 
	SqlSession ss = null;

	//글 저장
	public int insertBoard(Shop_qaVO shop_qa) {
		int result=0;
		try {
			ss = factory.openSession();
			result=ss.insert("qa.insertBoard", shop_qa);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return result;
	}
	
	// 금칙어 체크
	public int spamWords(Shop_qaVO spamTest) {
		System.out.println("dao:"+spamTest);
		int spamCheck = 0; // 스팸체크 플래그. 0이면 통과, 아니면 스팸
		try {
			ss = factory.openSession();			
			spamCheck = ss.selectOne("qa.spamCheck", spamTest);						
			ss.commit();			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return spamCheck;
	}
	

	//전체 게시물 수 가져오기
	public int getTotal(String searchText,int shop_seq){
		int total = 0;
		
		//Mybatis로 전달할 Parameter들
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("searchText", searchText);
		String shop_seq2 = Integer.toString(shop_seq);
		map.put("shop_seq", shop_seq2);
		try {
			ss = factory.openSession();
			total = ss.selectOne("qa.getTotal", map);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return total;
	}
	
	//글 목록
	public List<Shop_qaVO> listBoard(String searchText, Integer startRecord, Integer countPerPage, int shop_seq) {		
		List<Shop_qaVO> qaList = null;
		
		//Mybatis로 전달할 Parameter들
		HashMap<String, Object> map = new HashMap<>();
		map.put("searchText", searchText);
		map.put("startRecord", startRecord);
		map.put("countPerPage", countPerPage);
		map.put("shop_seq", shop_seq);
		
		System.out.println("map: "+map);
		//결과중 읽을 위치와 개수
		//RowBounds bound = new RowBounds(startRecord, countPerPage);
		
		try {
			ss = factory.openSession();
			qaList = ss.selectList("qa.listBoard", map);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			ss.close();
		}
		return qaList;
	}	
	
	//글 읽기
	public Shop_qaVO getBoard(int qa_seq) {
		System.out.println("겟보드 시작");
		Shop_qaVO qa = null;
		
		try {
			ss = factory.openSession();
			qa = ss.selectOne("qa.getBoard", qa_seq);
			ss.commit();
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			ss.close();
		}
		System.out.println("겟보드//action 직전qa: "+qa);
		return qa;
	}	
	
	//글 삭제
	public int deleteBoard(int qa_seq) {
		int result = 0;
		try {
			ss = factory.openSession();	
			result = ss.delete("qa.deleteBoard", qa_seq);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return result;
	}
	
	//글 수정
	public int updateBoard(Shop_qaVO shop_qa) {
		int result = 0;
		try {
			ss = factory.openSession();	
			result = ss.update("qa.updateBoard", shop_qa);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return result;
	}
	
	/*  리플 관련  */

	//답글 저장
	public void insertReply(Qa_commentVO vo) {
		try {
			ss = factory.openSession();
			System.out.println("리플저장,매퍼 전 vo :"+vo);
			ss.insert("qa.insertReply", vo);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
	}
	
	//한 게시물의 리플 목록
	public List<Qa_commentVO> listReply(int qa_seq) {
		List<Qa_commentVO> qa_commentList = null;
		
		try {
			ss = factory.openSession();
			qa_commentList = ss.selectList("qa.listReply", qa_seq);
			System.out.println("리플리스트 메퍼 이후 : "+qa_commentList);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			ss.close();
		}		
		return qa_commentList;
	}
	
	//리플 삭제
	public int deleteReply(int cmt_seq, String customer_id) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("cmt_seq", cmt_seq);
		map.put("customer_id", customer_id);
		
		int result = 0;
		try {
			ss = factory.openSession();	
			result = ss.delete("qa.deleteReply", map);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return result;
	}
	
	//리플 수정
	public int updateReply(Qa_commentVO qa_comment) {
		int result = 0;
		try {
			ss = factory.openSession();	
			result = ss.update("qa.updateReply", qa_comment);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return result;
	}
	
	//공지사항 가져오기
	public Shop_noticeVO getNotice(int qa_seq) {
		ss= factory.openSession();
		Shop_noticeVO vo = ss.selectOne("qa.noticeGet", qa_seq);
		return vo;
	}
}
